<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="dictName">
        <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="姓名" prop="dictName">
        <el-input v-model="queryParams.userName" placeholder="请输入姓名" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['health:health:add']">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="HealthList">
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="记录日期" align="center" prop="recordDate" />
      <el-table-column label="收缩压" align="center" prop="bloodPressureSystolic" />
      <el-table-column label="舒张压" align="center" prop="bloodPressureDiastolic" />
      <el-table-column label="心率" align="center" prop="heartRate" />
      <el-table-column label="体温" align="center" prop="bodyTemperature" />
      <el-table-column label="血糖值" align="center" prop="bloodGlucoseLevel" />
      <el-table-column label="血氧饱和度" align="center" prop="oxygenSaturation" />
      <el-table-column label="体重" align="center" prop="weight" />
      <el-table-column label="身高" align="center" prop="height" />
      <el-table-column label="睡眠时长" align="center" prop="sleepDuration" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['health:health:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['health:health:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户id">
          <el-input v-model="form.userId"  />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="记录日期">
          <el-date-picker v-model="form.recordDate" type="date" placeholder="选择日期时间" align="right"
                        value-format="yyyy-MM-dd" :picker-options="pickerOptions">
                    </el-date-picker>
        </el-form-item>
        <el-form-item label="收缩压">
          <el-input v-model="form.bloodPressureSystolic" />
        </el-form-item>
        <el-form-item label="舒张压">
          <el-input v-model="form.bloodPressureDiastolic" />
        </el-form-item>
        <el-form-item label="心率">
          <el-input v-model="form.heartRate" />
        </el-form-item>
        <el-form-item label="体温">
          <el-input v-model="form.bodyTemperature" />
        </el-form-item>
        <el-form-item label="血糖值">
          <el-input v-model="form.bloodGlucoseLevel" />
        </el-form-item>
        <el-form-item label="血氧饱和度">
          <el-input v-model="form.oxygenSaturation" />
        </el-form-item>
        <el-form-item label="体重">
          <el-input v-model="form.weight" />
        </el-form-item>
        <el-form-item label="身高">
          <el-input v-model="form.height" />
        </el-form-item>
        <el-form-item label="睡眠时长">
          <el-input v-model="form.sleepDuration" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { selectHealth, addHealth, editHealth, deleteHealth } from "@/api/health/health";
export default {
  name: "Health",
  data() {
    return {
      //显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      //总条数
      total: 0,
      // 选中数组
      ids: [],
      //查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        userId: undefined
      },
      //新增属性
      title: "",
      dialogVisible: false,
      form: {
        userId:undefined,
        userName: undefined,
        recordDate:undefined,
        bloodPressureSystolic: undefined,
        bloodPressureDiastolic: undefined,
        heartRate: undefined,
        bodyTemperature: undefined,
        bloodGlucoseLevel: undefined,
        oxygenSaturation: undefined,
        weight: undefined,
        height: undefined,
        sleepDuration: undefined,
        createBy:undefined,
        createTime:undefined,
        remark:undefined,
        id: undefined,
      },


      HealthList: [],
      // 表单校验
      rules: {

      },
          //
          pickerOptions: {
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },

    };
  },
  created() {
    this.getList();

  },

  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },


    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        userId: undefined,
      },
        this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询信息 */
    getList() {
      selectHealth(this.queryParams).then(response => {
        this.HealthList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.dialogVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.dialogVisible = true;
      this.form = row;
  },
    // 取消按钮
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editHealth(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.dialogVisible = false;
              this.getList();
            });
          } else {
            addHealth(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.dialogVisible = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const id = row.id;
      this.$modal.confirm('是否确认删除用户为"' + id + '"的数据项？').then(function () {
        return deleteHealth(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        userName: undefined,
        recordDate: undefined,
        bloodPressureSystolic: undefined,
        bloodPressureDiastolic: undefined,
        heartRate: undefined,
        bodyTemperature: undefined,
        bloodGlucoseLevel: undefined,
        oxygenSaturation: undefined,
        weight: undefined,
        height: undefined,
        sleepDuration: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },

  }
};
</script>
<style scoped></style>
